package com.jsnkj.basic.util;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class AppUtil {
    /**
     * 判断 用户是否安装微信客户端
     */
    public static Boolean isWeixinApplication(Context context) {
        return isPackageApplication(context, "com.tencent.mm");
    }


    /**
     * 判断应用是否存在
     *
     * @param context     上下文
     * @param packageName 应用包名
     */
    private static boolean isPackageApplication(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        boolean hasInstall;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_GIDS);
            hasInstall = packageInfo != null;
        } catch (PackageManager.NameNotFoundException e) {
            hasInstall = false;
            e.printStackTrace();
        }
        return hasInstall;
    }

    /**
     * 获取app的名称
     */
    public static String getAppName(Context context) {
        String appName = "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            appName = context.getResources().getString(labelRes);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return appName;
    }

    /**
     * 获取版本号
     */
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            String versionName = packInfo.versionName;
            int versionCode = packInfo.versionCode;
            return versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取手机型号
     */
    public static String getDeviceModel() {
        return Build.MODEL;
    }


    /**
     * 复制到剪贴板
     */
    public static void copyClipboard(Context context, String text) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setText(text);
    }


    /**
     * 获取渠道名
     *
     * @return 如果没有获取成功，那么返回值为空
     */
    public static String getChannelName(Context mContext) {
        if (mContext == null) {
            return null;
        }
        String channelName = null;
        try {
            PackageManager packageManager = mContext.getPackageManager();
            if (packageManager != null) {
                //注意此处为ApplicationInfo 而不是 ActivityInfo,因为友盟设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo.metaData != null) {
                    channelName = String.valueOf(applicationInfo.metaData.get("UMENG_CHANNEL"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e("获取渠道信息失败： " + e.toString());
        }
        return channelName;
    }


    public static String getAPPUUID(Context context) {

        String deviceId = "";
        try {
            deviceId = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);

            if (Build.VERSION.SDK_INT < 29) {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null) {
                    if (!TextUtils.isEmpty(tm.getDeviceId()))
                        deviceId = tm.getDeviceId();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deviceId;
    }

}


















